<?php
/**
 * @file entry.php
 * @package news
 * @author LD
 * @desc 入口
 */
$module = $_GET['module'];
$action = $_GET['action'];
$nid = $_GET['nid'];
$page = $_GET['page'];


/*$module = 'image';
$action = 'list';*/


require_once 'settings.php';

$smarty = new Smarty();
$smarty->template_dir = _TEMPLATE_DIR;
$smarty->compile_dir = _COMPILE_DIR;
$smarty->config_dir = _CONFIG_DIR;
$smarty->cache_dir = _CACHE_DIR;
$smarty->left_delimiter = _LEFT_DELIMITER;
$smarty->right_delimiter = _RIGHT_DELIMITER;
$smarty->assign('_TINYMCE_DIR', _TINYMCE_DIR);


	$smarty->assign("action", $action);
	
	$dbh = new kyodb();
	
		/* 分类 */
	$cats = $dbh->getCategories();
	$cat_options = array();
	foreach ($cats as $cat){
		$cat_options[$cat['ID']] = $cat['name'];
	}
	$smarty->assign("cat_options", $cat_options);
	
if ($module == 'news') {
		
		/* 发布者 */
	$auhs = $dbh->getAuthors();
	$auh_options = array();
	foreach ($auhs as $auh){
		$auh_options[$auh['ID']] = $auh['name'];
	}
	$smarty->assign("auh_options", $auh_options);
		
	if ($action == 'add'){
		$smarty->assign("page_title", "添加活动公告");
		$smarty->assign("cat_id", $cats[0]['ID']);
		$smarty->assign("auh_id", $auhs[0]['ID']);
		if ( $_POST['newsSubmit'] == '添加公告' ){
			$post = array();
			$post['author_id'] = $_POST['newsAuthor'];
			$post['category_id'] = $_POST['newsCats'];
			$post['title'] = trim($_POST['newsTitle']);		
			$post['content'] = $_POST['newsContent'];
			
			$xTag = $_POST['newsTags'];
			$xTag = makeTags($xTag);
			$tags = array( implode(' ',$xTag) );
			
			if ( empty($post['content']) or empty($post['title']) ) {
				$smarty->assign('error', "活动标题 和 内容 不能为空！");		
			}else{
				$ID = $dbh->insertPost($post);
				if ($ID == 0) $smarty->assign('error', "公告插入失败！");
				else{
					$dbh->insertTags($ID, $tags);
					$smarty->assign('success', "【{$ID}】{$post['title']}");
				}
			}
		}//END 提交表单
		$smarty->display('news_post.html');
		
	}elseif ($action == 'edit'){
		$smarty->assign("page_title", "编辑活动公告");
		if (empty($nid)) $smarty->assign('error', "没有传入 活动公告 的ID！");
		else{
			$smarty->assign('nid', $nid);
			if ( $_POST['newsSubmit'] == '编辑公告' ){
				$post = array();
				$post['author_id'] = $_POST['newsAuthor'];
				$post['category_id'] = $_POST['newsCats'];
				$post['title'] = trim($_POST['newsTitle']);		
				$post['content'] = $_POST['newsContent'];
				
				$xTag = $_POST['newsTags'];
				$xTag = makeTags($xTag);
				$tags = array( implode(' ',$xTag) );
				
				if ( empty($post['content']) or empty($post['title']) ) {
					$smarty->assign('error', "活动标题 和 内容 不能为空！");		
				}else{
					$dbh->updateNews($nid, 'post', $post);
					$dbh->updateNews($nid, 'tag', $tags);
					$smarty->assign('success', "【{$nid}】{$post['title']}");
				}
			}//END 提交表单
			
			$post = $dbh->getPost($nid);
			$smarty->assign('xTitle', $post['title'] );
			$smarty->assign('xContent', $post['content'] );
			$smarty->assign("cat_id", $post['category_id'] );
			$smarty->assign("auh_id", $post['author_id'] );
			
			$xTag = $dbh->getTag($nid);
			$smarty->assign('xTag', $xTag['name']);
			$smarty->display('news_post.html');
		}
	}elseif ($action == 'list' or $action == 'delete'){
		if ( $action == 'delete' and !empty($nid) ){
			if ( $dbh->deleteNews($nid, 'post') )
				$smarty->assign('success', "DELETE {$nid}");
			else
				$smarty->assign('error', "DELETE {$nid}");
		}
		
		$smarty->assign("page_title", "活动公告列表");
		
		if (empty($page)) $page = 1;
		else $page = (int)$page;
		$smarty->assign('page', $page);
		$sum = $dbh->getSum('post');
		$sum = intval($sum/10);
		if ( $sum%10 > 0  or $sum == 0 ) $sum++;
		
		$smarty->assign('sum', $sum);
		/*if($page>1) $smarty->assign('prev', $page-1);
		else $smarty->assign('next', $page+1);*/
		$page = ($page - 1)*10;
		
		$posts = $dbh->getPosts($page);
		$xPosts = array();
		foreach ( (array)$posts as $post){
			$xPost = array('ID' => $post['ID']);
			$xPost['category'] = $cat_options[$post['category_id']];
			$xPost['title'] = $post['title'].'&nbsp;&nbsp;('.$post['date'].')';
			$xPost['edit'] = "<a href=\"entry.php?module=news&action=edit&nid={$post['ID']}\">编辑</a>";	
			$xPost['delete'] = "<a href=\"entry.php?module=news&action=delete&nid={$post['ID']}\">删除</a>";
			array_push($xPosts, $xPost);
		}
		
		if ( !empty($posts) ){
			$smarty->assign('xPosts', $xPosts);
		}
				
		$smarty->display('news_postList.html');
	}else  {
		die("Action Error!");
	}
}//module 'news'
/* -------------------------- --- Category --- -------------------------- */
elseif ($module == 'cat') {
	
	if ($action == 'add' or $action == 'edit'){
		if ($_POST['catSubmit'] == '提交分类'){
			$cat = array();
			$cat['name'] = $_POST['catName'];
			$cat['desc'] = $_POST['catDesc'];
			if (empty($cat['name'])) $smarty->assign('error', "分类名称不能为空~");
			else {
				if ($action == 'add'){	
					$ID = $dbh->insertCategory($cat);
					if ( empty($ID) ) $smarty->assign('error', "分类插入失败~");
					else $smarty->assign('success', "【{$ID}】{$cat['name']}");
				}      //acrion add ENF
				elseif ($action == 'edit'){
					$nid = $_GET['nid'];
					if (empty($nid)) $smarty->assign('error', "没有传入 公告分类 的ID！");
					else {
						$dbh->updateNews($nid, 'cat', $cat);
						$smarty->assign('success', "【{$nid}】{$cat['name']}");
					}
				} //acrion eidt ENF
			}
			
		}//END 提交表单
		
		if ($action == 'edit' and !empty($nid) ){
			$cat = $dbh->getCategories($nid);
			$cat = $cat[0];
			$smarty->assign('xName', $cat['name']);
			$smarty->assign('xDesc', $cat['desc']);
			$smarty->assign('nid', $nid);
		}//END action edit
		
		$smarty->display('news_cat.html');
		
	}elseif ($action == 'list' or $action == 'delete'){
		if ( $action == 'delete' and !empty($nid) ){
			if ( $dbh->deleteNews($nid, 'cat') )
				$smarty->assign('success', "DELETE Category {$nid}");
			else
				$smarty->assign('error', "DELETE Category {$nid}");
		}
		
		$smarty->assign("page_title", "公告分类列表");
		
		$cats = $dbh->getCategories();
		$xCats = array();
		foreach ($cats as $cat){
			$xCat = array('ID' => $cat['ID']);
			$xCat['name'] = $cat['name'];
			$xCat['desc'] =$cat['desc'];
			$xCat['edit'] = "<a href=\"entry.php?module=cat&action=edit&nid={$cat['ID']}\">编辑</a>";	
			$xCat['delete'] = "<a href=\"entry.php?module=cat&action=delete&nid={$cat['ID']}\">删除</a>";
			array_push($xCats, $xCat);
		}
		
		if ( !empty($xCats) ){
			$smarty->assign('xPosts', $xCats);
		}
		$smarty->display('news_catList.html');
		
	}else {
		die("Action Error!");
	}
}//module 'cat'
/* -------------------------- --- Image --- -------------------------- */
elseif ($module == 'image'){
	if ($action == 'upload'){
		$filename = upFile();
		if ( empty($filename) ) $smarty->assign('error', "upload file !");
		else {
			$filename = str_replace($_SERVER['DOCUMENT_ROOT'], _SITEURL, $filename);
			$smarty->assign('success', "upload: {$filename}");
			$filename = str_replace(_SITEURL, '', $filename);
			$dbh->insertImage($filename);			
		}
	}//action upload end
	elseif ($action == 'delete'){
		if (empty($nid))   $smarty->assign('error', "ID号为空~");
		else 
			$dbh->deleteNews($nid, 'image');
	}
	
		$smarty->assign("page_title", "管理图片");
		
		if (empty($page)) $page = 1;
		else $page = (int)$page;
		$smarty->assign('page', $page);
		$sum = $dbh->getSum('image');
		$sum = intval($sum/10); 
		if ( $sum%10 > 0 or $sum == 0 ) $sum++;
		$smarty->assign('sum', $sum);
		$page = ($page - 1)*10;
	
		$images = $dbh->getImages($page);
		$xImages = array();
		foreach ($images as $image){
			$xImage = array('ID' => $image['ID']);
			$xImage['url'] = _SITEURL.$image['url'];
			$xImage['look'] = "<a target=\"_blank\" href=\"{$xImage['url']}\">查看</a>";	
			$xImage['delete'] = "<a href=\"entry.php?module=image&action=delete&nid={$image['ID']}\">删除</a>";
			array_push($xImages, $xImage);
		}
		
		if ( !empty($images) ){
			$smarty->assign('xPosts', $xImages);
		}
	
	$smarty->display('news_image.html');
}//module 'image'



?>